module executor.rpc.client.RpcResponseExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import client.ImClient;
import std.array;
import command.StartAppCommand;
import std.conv: to;
import hunt.logging;
import std.json;
import core.stdc.string;
import client.RpcRequestsMap;

@TransportClient(logicClientName)
class RpcResponseExecutor : AbstractExecutor!(RpcResponseExecutor){
  @MessageId(MESSAGE.CMD_RPC_RESPONST)
  void rpcResponse(TransportContext ctx, MessageBuffer buffer)
  {
    auto res = new RPC_RESPONSE();
    buffer.data.fromProtobuf!RPC_RESPONSE(res);

    ServerExtend serverExtend ;//= cast(ServerExtend)(buffer.extend);
    memcpy(&serverExtend,buffer.extend.ptr,ServerExtend.sizeof);

    RpcRequestsMap.getInstance().deleRequest(serverExtend.userId,serverExtend.requestId,res.src);

    TransportSession client = ImClient.getInstance().getImClientByMixId(ImClient.getInstance().idMixSrc(serverExtend.userId, res.src));
    if (client !is null)
    {
      ClientExtend clientExtend;
      clientExtend.requestId = serverExtend.requestId;

      ubyte[ClientExtend.sizeof] extend;
      memcpy(extend.ptr,&clientExtend,ClientExtend.sizeof);
      client.send( new MessageBuffer( MESSAGE.CMD_RPC_RESPONST,res.toProtobuf.array,extend));
    }
  }
}

